package cn.usian.controller;

import cn.jiyun.UserClient;
import cn.jiyun.pojo.Order;
import cn.jiyun.pojo.User;
import cn.jiyun.service.OrderService;
import cn.usian.pojo.Order;
import cn.usian.service.OrderService;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private UserClient userClient;

    @GetMapping
    public List<Order> findAll(){
          List<Order> list= orderService.findAll();
        for (Order order : list) {
            User user=userClient.findById(order.getUserId());
            order.setUser(user);
        }
        return list;
    }

    @PostMapping("/add")
    public void  add(@RequestBody Order order){
        User user = order.getUser();
        User user1= userClient.add(user);

        order.setUserId(user1.getId());
        orderService.add(order);
    }

    @GetMapping("/findById")
    public Order findById(Long id){
        Order order=orderService.findById(id);
        User user = userClient.findById(order.getUserId());
        order.setUser(user);
        return order;
    }

    @GetMapping("/del")
    public void  del(Long id){
        Order order = orderService.findById(id);
        User user = userClient.findById(order.getUserId());
        Long uid = user.getId();
        userClient.del(uid);
        orderService.del(id);
    }
}
